package com.ali.juejin.model

import androidx.lifecycle.MutableLiveData
import com.ali.juejin.base.BaseModelView
import com.ali.juejin.bean.article.Comments
import com.ali.juejin.bean.home.FollowBean
import com.ali.juejin.bean.v1.Data
import com.ali.juejin.bean.v1.ItemInfo
import com.ali.juejin.bean.v1.article.comment.CommentBean
import com.ali.juejin.bean.v1.article.comment.RequestPageBean
import com.ali.juejin.bean.v1.article.detail.RequestArticle
import com.ali.juejin.bean.v1.article.detail.RequestRelatedArticle
import com.ali.juejin.config.UrlConfigV1
import com.ali.juejin.services.ArticleDetailServices
import com.ali.juejin.utils.Request
import com.google.gson.Gson
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

class ArticleDetailModel : BaseModelView(){

    val net by lazy { Request.instance(UrlConfigV1.RECOMMENDAPI) }
    val net1 by lazy { Request.instance(UrlConfigV1.COMMENTAPI) }
    val netCommentList by lazy { Request.instance(UrlConfigV1.INTERACTAPI) }

    val detailInfo by lazy { MutableLiveData<ItemInfo>() }
    val relatedEntryList by lazy { MutableLiveData<ArrayList<ItemInfo>>() }
    val commentList by lazy { MutableLiveData<CommentBean>() }

    // 详情
    fun getDetailInfo(entryIds:String){
        val requestInfo = RequestArticle(entryIds,2606)
        val info = Gson().toJson(requestInfo)
        val body = RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(),info)
        request<FollowBean>({
            val data = net1.create(ArticleDetailServices::class.java).getDetailInfo(body)
//            LogUtils.wTag("结果",data)
            detailInfo.postValue(data.data)
        })
    }


//    相关文章
    fun getRelatedEntry(entryId:String, tag_ids: ArrayList<String>, user_id:String){
        val requestInfo = RequestRelatedArticle(2606,"0",2,entryId,3,tag_ids,user_id)
        val info = Gson().toJson(requestInfo)
        val body = RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(),info)
        request<FollowBean>({
            val data = net.create(ArticleDetailServices::class.java).getRelatedEntry(body)
//            LogUtils.wTag("结果",data)
            relatedEntryList.postValue(data.data)
        })

    }

//    评论
    fun getCommentList(entryId:String){
        if(commentList.value?.count == 0) return
        val requestInfo = RequestPageBean(2606,commentList.value?.cursor?:"0",entryId,2,20)
        val info = Gson().toJson(requestInfo)
        val body = RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(),info)
        request<Comments>({
            val data = netCommentList.create(ArticleDetailServices::class.java).getCommentList(body)

            val dataList = data.data
            val tempList = commentList.value?.data
            if(tempList.isNullOrEmpty()){
                commentList.postValue(data)
            }else{
                val tempRes = commentList.value
                tempRes?.data?.addAll(dataList)
                tempRes?.cursor = data.cursor
                commentList.postValue(tempRes)
            }

        })

    }
}